<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "user".
 *
 * @property integer $id
 * @property string $username
 * @property string $auth_key
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $email
 * @property integer $status
 * @property string $created_at
 * @property string $updated_at
 */
class User extends superModel {

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    public static function getCTableName()
    {
        return '用户';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
                [ [ 'username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at' ], 'required' ],
                [ [ 'status' ], 'integer' ],
                [ [ 'created_at', 'updated_at' ], 'safe' ],
                [ [ 'username', 'password_hash', 'password_reset_token', 'email' ], 'string', 'max' => 255 ],
                [ [ 'auth_key' ], 'string', 'max' => 32 ],
                [ [ 'username' ], 'unique' ],
                [ [ 'email' ], 'unique' ],
                [ [ 'password_reset_token' ], 'unique' ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id'                   => 'ID',
            'username'             => '用户名',
            'auth_key'             => 'Auth Key',
            'password_hash'        => '密码',
            'password_reset_token' => 'Password Reset Token',
            'email'                => '邮箱',
            'status'               => 'Status',
            'created_at'           => '创建时间',
            'updated_at'           => '修改时间',
        ];
    }

    public function seekPass($data)
    {
        $model = self::findOne([ 'username' => $data['User']['username'], 'email' => $data['User']['email'] ]);
        if (isset($model)) {
            //做点有意义的事
            $time = time();
            $token = $this->createToken($data['User']['username'], $time);
            $mailer = Yii::$app->mailer->compose('seekpass', [ 'username' => $data['User']['username'], 'time' => $time, 'token' => $token ]);
            $mailer->setFrom("13132693017@163.com");
            $mailer->setTo($data['User']['email']);
//            $mailer->setTo($data['User']['email']);
            $mailer->setSubject("桂智商城-找回密码");
            if ($mailer->send()) {
                return true;
            }
            else {
                return FALSE;
            }
        }
        return false;
    }

    public function createToken($adminuser, $time)
    {
        $token = md5(md5($adminuser) . base64_encode(Yii::$app->request->userIP) . md5($time));
        return $token;
    }

//    public function sendMail()
//    {
//        $mailer = Yii::$app->mailer->compose()
//                ->setFrom('13132693017@163.com')
//                ->setTo('13132693017@163.com')
//                ->setSubject('Message subject')
////                ->setTextBody('Plain text content')
//                ->setHtmlBody('<b>HTML content</b>');
//
//        echo "<pre>";
//        var_dump($mailer->send());
//        echo "</pre>";
//        exit;
//    }
    
    
    public function setCreateTime()
    {
        $this->created_at = $this->getDateTime();
    }
    
    public function setUpdateTime()
    {
        $this->updated_at = $this->getDateTime();
    }

    public function cryptPassword($password)
    {

        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
    }

    public function beforeSave($insert)
    {

        if (parent::beforeSave($insert)) {
            if ($this->isNewRecord) {
                $this->setCreateTime();
            }
            $this->setUpdateTime();
        }
        return TRUE;
    }

}
